home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1996 / MacHack 1996.toast / Hacks / Hacks ’90 / Surovell Stuffƒ / VBLƒ / VBLTasks.c next >
Encoding:
C/C++ Source or Header  |  1990-06-15  |  3.4 KB  |  181 lines  |  [TEXT/KAHL]

  1. /*
  2.     Copyright © 1988,1989,1990 by
  3.         David A. Surovell,
  4.         Succinct Systems
  5.         433 Huronview
  6.         Ann Arbor, MI 48103
  7.         (313) 663-4903
  8.         AppleLink: (none yet)
  9.  
  10.     ••• All Rights Reserved. •••
  11.  
  12.     File:         VBLTasks.c
  13.     Model:         THINK C 4.0, MPW C 3.0, Aztec C 3.0
  14.  
  15.     ABSTRACT:
  16.         vertical-blank task handling routines.
  17.  
  18.     NOTES:
  19.         ---
  20.  
  21.     KNOWN BUGS:
  22.         ---
  23.  
  24.     HISTORY:
  25.         DAS    01-Jun-90    did it here first
  26. */
  27.  
  28. #include <Color.h>
  29. #include <DeviceMgr.h>
  30. #include <SlotMgr.h>
  31. #include <VRetraceMgr.h>
  32. #include "VBLTasks.h"
  33.  
  34. static ProcPtr    curVBHandler = NULL;    
  35. static Boolean    vbTaskStatus = FALSE;    
  36.  
  37.  
  38. /* MPW C glue, because there is no inline assembler */
  39. #ifdef _MPWC_
  40. VBLTaskInfo    *GetVBLInfo( void ) = { 0x2008 };        /* MOVE.L    A0,D0 */
  41. #endif _MPWC_
  42.  
  43.  
  44. /*
  45. **    install generic task stub on specified GDevice:
  46. **        task info ptr returned on success
  47. **        NULL returned on failure
  48. */
  49. VBLTaskInfoPtr InstallVBTask(
  50.     GDHandle    theGDev )
  51. {
  52. SysEnvRec        theWorld;
  53. QElemPtr        theQueueItem;
  54. VBLTaskInfoPtr    curVBLInfo;
  55. VBLTask            *vbInt;
  56. OSErr            errStat;
  57. unsigned short    curSlotID;
  58.  
  59.     (void)SysEnvirons( curSysEnvVers, &theWorld );
  60.  
  61.     curVBLInfo = (VBLTaskInfoPtr)NewPtrClear( sizeof(VBLTaskInfoRec) );
  62.     if (curVBLInfo == NULL)
  63.         return NULL;
  64.  
  65.     /* facilitates “better” access to private VB info */
  66.     vbInt = (VBLTask*)curVBLInfo;
  67.     vbInt->qType = vType;
  68.     vbInt->vblAddr = (ProcPtr)(&DispatchVBTask);
  69.  
  70.     /* execute every VBLANK_RATE vertical blanks */
  71.     vbInt->vblCount = VBLANK_RATE;
  72.     vbInt->vblPhase = 0;
  73.     curVBLInfo->A5World = SetCurrentA5();
  74.  
  75.     theQueueItem = (QElemPtr)curVBLInfo;
  76.     if (theWorld.hasColorQD)
  77.     {
  78.         if (theGDev == NULL)
  79.             theGDev = GetMainDevice();
  80.  
  81.         /* install VBL on slot of main device (screen with menu bar) */
  82.         curSlotID = GDGetSlotID( theGDev );
  83.         errStat = SlotVInstall( theQueueItem, curSlotID );
  84.     }
  85.     else
  86.         /* old black & white Mac */
  87.         errStat = VInstall( theQueueItem );
  88.  
  89.     if (errStat == noErr)
  90.     {
  91.         vbTaskStatus = TRUE;
  92.         return curVBLInfo;
  93.     }
  94.     else
  95.     {
  96.         vbTaskStatus = FALSE;
  97.         DisposPtr( (Ptr)curVBLInfo );
  98.         return NULL;
  99.     }
  100. }
  101.  
  102.  
  103. /*
  104. **    obtain slot number of specified GDevice
  105. */
  106. unsigned short GDGetSlotID(
  107.     GDHandle    theGDev )
  108. {
  109. AuxDCEHandle    theDrvrHdl;
  110. short            slotNum;
  111.  
  112.     if (theGDev != NULL)
  113.     {
  114.         theDrvrHdl = (AuxDCEHandle)GetDCtlEntry( (**theGDev).gdRefNum );
  115.         if (theDrvrHdl != NULL)
  116.             return (**theDrvrHdl).dCtlSlot;
  117.     }
  118.  
  119.     return 0x0000;
  120. }
  121.  
  122.  
  123. /*
  124. **    set A5 context, dispatch VBTask, refresh vbl counter
  125. **    including additional task handler
  126. */
  127. pascal void DispatchVBTask( void )
  128. {
  129. VBLTaskInfoPtr    curVBLInfo;
  130. unsigned long    oldA5;
  131.  
  132. #ifdef THINK_C
  133.     asm { move.l    A0, curVBLInfo }
  134. #endif THINK_C
  135.  
  136. #ifdef _MPWC_
  137.     curVBLInfo = GetVBLInfo();
  138. #endif _MPWC_
  139.  
  140.     /* allow global variables to be properly referenced */
  141.     oldA5 = SetA5( curVBLInfo->A5World );
  142.  
  143.     /* if task flag is still set, handle the VBL signal */
  144.     if (vbTaskStatus)
  145.     {
  146.         /* reset the task counter so that this task will stay active */
  147.         curVBLInfo->vbl.vblCount = VBLANK_RATE;
  148.  
  149.         /* perform any additional tasks here */
  150.         /* remember not to perform any memory management functions */
  151.         if (curVBHandler != NULL)
  152.             (*curVBHandler)();
  153.     }
  154.  
  155.     /* restore original A5 value */
  156.     (void)SetA5( oldA5 );
  157. }
  158.  
  159.  
  160. /*
  161. **    install a routine to handle additional VB task activities:
  162. **    must be of type "void FunctionName( void )"
  163. */
  164. void SetVBHandler(
  165.     ProcPtr        theVBHandler )
  166. {
  167.     curVBHandler = theVBHandler;    
  168. }
  169.  
  170.  
  171. /*
  172. **    set flag to enable a VBTask to renew itself;
  173. **    clear to kill a running VBTask
  174. */
  175. void SetVBLTaskStatus(
  176.     Boolean        newTaskStatus )
  177. {
  178.     vbTaskStatus = newTaskStatus;    
  179. }
  180.  
  181.